package com.newdjk.newdoctor.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ajguan.library.EasyRefreshLayout;
import com.ajguan.library.LoadModel;
import com.chad.library.adapter.base.BaseNodeAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.newdjk.newdoctor.BasActivity;
import com.newdjk.newdoctor.MyApplication;
import com.newdjk.newdoctor.R;
import com.newdjk.newdoctor.adapter.OrderListAdapter;
import com.newdjk.newdoctor.adapter.ShopListAdapter;
import com.newdjk.newdoctor.net.BaseComPleteObserver;
import com.newdjk.newdoctor.net.NetServices;
import com.newdjk.newdoctor.utils.Event;
import com.newdjk.newdoctor.utils.RxBus;
import com.newdjk.okhttp.entity.BaseEntity;
import com.newdjk.okhttp.entity.OrderListEntity;
import com.newdjk.okhttp.entity.ShopListEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 切换门店
 */
public class SwitchShopActivity extends BasActivity {


    private static final String TAG = "SwitchShopActivity";
    @BindView(R.id.recyleview)
    RecyclerView recyleview;
    @BindView(R.id.easylayout)
    EasyRefreshLayout easylayout;
    @BindView(R.id.iv_no)
    ImageView ivNo;
    @BindView(R.id.mNodataContainer)
    RelativeLayout mNodataContainer;
    private ArrayList<ShopListEntity.ReturnDataBean> mOrderList = new ArrayList<>();
    private int pageindex = 1;
    private ShopListAdapter mOrderListAdapter;


    @Override
    protected String setTitle() {
        return "切换门店";
    }

    @Override
    protected int initViewResId() {
        return R.layout.activity_switch_shop;
    }


    @Override
    protected void initListener() {
        easylayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            @Override
            public void onLoadMore() {
                GetMyMedicationCompanyList();
            }

            @Override
            public void onRefreshing() {
                pageindex = 1;
                mOrderList.clear();
                easylayout.setLoadMoreModel(LoadModel.COMMON_MODEL);
                GetMyMedicationCompanyList();
            }
        });

        mOrderListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {


                NetServices.Factory.getService().ChangeMyMedicationCompany(
                        MyApplication.LoginEntity.getUser().getDoctorId() + "",
                        mOrderList.get(position).getMedicationCompanyId()+""

                ).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseComPleteObserver(SwitchShopActivity.this, true) {
                            @Override
                            protected void onSuccees(BaseEntity t) throws Exception {

                                RxBus.get().post(Event.Switch_shop_success, true);
                                finish();

                            }

                            @Override
                            protected void onFailure(Throwable e, String erroressage, boolean isNetWorkError) throws Exception {
                                super.onFailure(e, erroressage, isNetWorkError);

                            }
                        });
            }

        });
    }


    @Override
    protected void initData() {

        GetMyMedicationCompanyList();
    }

    private void GetMyMedicationCompanyList() {

        if (MyApplication.isLogin) {
            NetServices.Factory.getService().GetMyMedicationCompanyList(
                    MyApplication.LoginEntity.getUser().getDoctorId() + "",
                    pageindex,
                    10
            ).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseComPleteObserver<ShopListEntity>(this, true) {
                        @Override
                        protected void onSuccees(BaseEntity<ShopListEntity> t) throws Exception {
                            Log.d(TAG, t.getData().getReturnData() + "");
                            if (pageindex != 1) {
                                easylayout.loadMoreComplete();
                            } else {
                                easylayout.refreshComplete();
                            }

                            List<ShopListEntity.ReturnDataBean> returnlist = t.getData().getReturnData();
                            if (returnlist != null) {
                                if (returnlist.size() == 10) {  //还有数据
                                    pageindex++;
                                    mOrderList.addAll(returnlist);
                                    mOrderListAdapter.setNewInstance(mOrderList);
                                    mOrderListAdapter.notifyDataSetChanged();
                                } else if (returnlist.size() >= 0 && returnlist.size() < 10) { //加载完成
                                    mOrderList.addAll(returnlist);
                                    mOrderListAdapter.setNewInstance(mOrderList);
                                    easylayout.setLoadMoreModel(LoadModel.NONE);
                                    mOrderListAdapter.notifyDataSetChanged();
                                }
                                //  reFreshNameLetter(returnlist);

                            } else {
                                easylayout.setLoadMoreModel(LoadModel.NONE);
                            }
                            if (mOrderList.size() <= 0) {
                                Log.i("MessageFragment", "lenError");
                                // mEasylayout.setVisibility(View.GONE);
                                recyleview.setVisibility(View.GONE);
                                mNodataContainer.setVisibility(View.VISIBLE);
                            } else {
                                //  mEasylayout.setVisibility(View.VISIBLE);
                                recyleview.setVisibility(View.VISIBLE);
                                mNodataContainer.setVisibility(View.GONE);
                            }

                        }

                        @Override
                        protected void onFailure(Throwable e, String erroressage, boolean isNetWorkError) throws Exception {
                            super.onFailure(e, erroressage, isNetWorkError);
                            easylayout.loadMoreFail();
                            if (mOrderList.size() <= 0) {
                                Log.i("MessageFragment", "lenError");
                                // mEasylayout.setVisibility(View.GONE);
                                recyleview.setVisibility(View.GONE);
                                mNodataContainer.setVisibility(View.VISIBLE);
                            } else {
                                //  mEasylayout.setVisibility(View.VISIBLE);
                                recyleview.setVisibility(View.VISIBLE);
                                mNodataContainer.setVisibility(View.GONE);
                            }

                        }
                    });
        }
    }


    @Override
    protected void initView() {
        mOrderListAdapter = new ShopListAdapter(mOrderList);
        recyleview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyleview.setAdapter(mOrderListAdapter);
    }

    @Override
    public boolean IsShowBackIcon() {
        return true;
    }



}
